package ar.republicagroup.reportes.util;

import ar.republicagroup.reportes.bean.ReporteComGestionLiquidBean;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class Export
{
    
    private static Logger log = Logger.getLogger(Export.class);
    
    
    public static void a_Excel(String archivo, List<ReporteComGestionLiquidBean> reporteList,Map params)
  {
      
       final String GASTOS_DIARIO = "Gastos diarios";
       final String GASTOS_BANCO = "Gastos bancos";
      
      
    try
    {
      log.info("Exportando a Excel...");  
      InputStream inputStream = Export.class.getClassLoader().getResourceAsStream("ar/republicagroup/reportes/config/ReporteLiquidDiario.xlt");  
      POIFSFileSystem fs = new POIFSFileSystem(inputStream);
      HSSFWorkbook workbook = new HSSFWorkbook(fs);
      HSSFSheet sheet = workbook.getSheetAt(0);
    
      SimpleDateFormat spanishFormat = new SimpleDateFormat("dd-MMMM-yyyy");
      
      /*Liquidación del día*/
      HSSFRow rowLiquidDelDia = sheet.getRow(4);
      HSSFCell cellLiquidDelDia = rowLiquidDelDia.createCell(1);
      cellLiquidDelDia.setCellValue(params.get("fechaReporte").toString());
      /*Hora y minutos*/
      HSSFRow rowHoraMins = sheet.getRow(5);
      HSSFCell cellHoraMins = rowHoraMins.createCell(1);
      cellHoraMins.setCellValue(obtenerHoraYMinutosDeDate(new Date()));
      
        
      /*fila inicial donde empiez a cargar la data*/
      int currentRow = 8;
      double totalComGestion = 0;
      double totalRechazos = 0;
      
      for (ReporteComGestionLiquidBean fila : reporteList)
      {
        HSSFRow row = sheet.createRow(currentRow);
        
        HSSFCell id = row.createCell(0);
        id.setCellValue(fila.getNombreCliente());
        
        HSSFCell comisionGestion = row.createCell(1);
        comisionGestion.setCellValue(Formato.decimalComaMilesConSigno(fila.getComisionPorGestion()));
        
        HSSFCell rechazo = row.createCell(2);
        rechazo.setCellValue(Formato.decimalComaMilesConSigno(fila.getRechazos()));
        
        totalComGestion += fila.getComisionPorGestion();
        totalRechazos   += fila.getRechazos();
        
        currentRow++;
      }
      
      currentRow++;
      
      HSSFRow totalComGestionRow = sheet.createRow(currentRow);
      HSSFCell totalComGestionLabel = totalComGestionRow.createCell(0);
      totalComGestionLabel.setCellValue("Gestión :");
      
      HSSFCell totalComGestionValue = totalComGestionRow.createCell(1);
      totalComGestionValue.setCellValue(Formato.decimalComaMilesConSigno(totalComGestion));
      
      HSSFCell totalRechazosValue = totalComGestionRow.createCell(2);
      totalRechazosValue.setCellValue(Formato.decimalComaMilesConSigno(totalRechazos));
      
      currentRow++;
      
      HSSFCellStyle style1 = workbook.createCellStyle();
      
      HSSFFont fontRed = workbook.createFont();
      fontRed.setColor(HSSFColor.RED.index);
      fontRed.setFontName("Calibri");
      fontRed.setFontHeightInPoints(Short.valueOf("11"));
      style1.setFont(fontRed);
      
            double gastoDiario = 0;
            double gastoBanco  = 0;
      
            if( params.get(GASTOS_DIARIO) != null &&  params.get(GASTOS_BANCO) != null){
               gastoDiario  = Double.parseDouble( params.get(GASTOS_DIARIO).toString() );
               gastoBanco   = Double.parseDouble( params.get(GASTOS_BANCO).toString() );
            }
            
            HSSFRow totalGastosDiariosRow = sheet.createRow(currentRow);
            HSSFCell totalGastosDiariosLabel = totalGastosDiariosRow.createCell(0);
            totalGastosDiariosLabel.setCellValue("Gastos diarios : ");

            HSSFCell totalGastosDiariosValue = totalGastosDiariosRow.createCell(1);
            totalGastosDiariosValue.setCellValue( Formato.decimalComaMilesConSigno( gastoDiario ) );
            totalGastosDiariosValue.setCellStyle( style1 );

            currentRow++;

            HSSFRow totalGastosBancoRow = sheet.createRow(currentRow);
            HSSFCell totalGastosBancoLabel = totalGastosBancoRow.createCell(0);
            totalGastosBancoLabel.setCellValue("Gastos bancos : ");

            HSSFCell totalGastosBancoValue = totalGastosBancoRow.createCell(1);
            
            totalGastosBancoValue.setCellValue( Formato.decimalComaMilesConSigno( gastoBanco ) );
            totalGastosBancoValue.setCellStyle( style1 );

            currentRow++;
      
      
      HSSFRow totalFinalBancoRow = sheet.createRow(currentRow);
      HSSFCell totalFinalLabel = totalFinalBancoRow.createCell(0);
      totalFinalLabel.setCellValue("Total final : ");
      
      HSSFCell totalFinalBancoValue = totalFinalBancoRow.createCell(1);
      double totalFinal = totalComGestion - gastoDiario - gastoBanco;
      totalFinalBancoValue.setCellValue( Formato.decimalComaMilesConSigno( totalFinal ));
      
      HSSFCellStyle style2 = workbook.createCellStyle();
       
      HSSFFont font = workbook.createFont();
      font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
      font.setFontHeightInPoints(Short.valueOf("11"));
      font.setFontName("Calibri");
      style2.setFont(font);
      totalFinalBancoValue.setCellStyle( style2 );
      
      FileOutputStream fileout = new FileOutputStream(archivo);
      workbook.write(fileout);
      fileout.close();
      
      log.info("Exportacion correcta!!! =)");
        
    }catch (Exception ex) {
        log.info("Se produjo un error al exportar...");
        log.error(ex.getMessage(), ex);
    }
  }
  
  
  
  
  
  public static String obtenerHoraYMinutosDeDate(Date fecha){
            String hora;
            String minutos;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(fecha);
         
          if(calendar.get(Calendar.HOUR_OF_DAY)< 10){
            hora = "0"+calendar.get(Calendar.HOUR_OF_DAY);
          }else{
            hora = ""+calendar.get(Calendar.HOUR_OF_DAY);  
            }
                  
          if (calendar.get(Calendar.MINUTE) < 10){
              minutos = "0"+calendar.get(Calendar.MINUTE);
          } else {
              minutos = ""+calendar.get(Calendar.MINUTE);
            }
         
         return hora + ":" +minutos;
     }
  
  
  
}
